﻿namespace = shogunate_alliance

# Setup Alliances (on game start)
# based on negotiate_alliance_interaction in /common/character_interactions/00_alliance.txt

shogunate_alliance.0001 = {
	scope = none
	hidden = yes

	immediate = {
		every_ruler = {
			save_scope_as = actor

			every_ruler = {
				limit = {
					NOT = { this = scope:actor }
					NOT = { is_allied_to = scope:actor }
				}
				save_scope_as = recipient

				#Prioritize marriage between rulers and/or direct blood relation between rulers first.
#				if = {
#					limit = {
#						scope:actor = {
#							OR = {
#								is_spouse_of = scope:recipient
#								is_child_of = scope:recipient
#								is_parent_of = scope:recipient
#								is_sibling_of = scope:recipient
#								is_grandparent_of = scope:recipient
#								is_great_grandparent_of = scope:recipient
#								is_grandchild_of = scope:recipient
#								is_great_grandchild_of = scope:recipient
#								is_uncle_or_aunt_of = scope:recipient
#								is_nibling_of = scope:recipient
#							}
#						}
#					}
#					scope:actor = {
#						create_alliance = {
#							target = scope:recipient
#							allied_through_owner = scope:actor
#							allied_through_target = scope:recipient
#						}
#					}
#				}
				#...Then marriage between close family members and either actor or recipient.
				if = {
					limit = {
						scope:recipient = {
							any_spouse = {
								OR = {
									is_close_family_of = scope:actor
									is_nibling_of = scope:actor
									is_uncle_or_aunt_of = scope:actor
								}
							}
						}
					}
					scope:recipient = {
						random_spouse = {
							limit = {
								OR = {
									is_close_family_of = scope:actor
									is_nibling_of = scope:actor
									is_uncle_or_aunt_of = scope:actor
								}
							}
							save_scope_as = recipient_spouse
						}
					}
					scope:actor = {
						create_alliance = {
							target = scope:recipient
							allied_through_owner = scope:recipient_spouse
							allied_through_target = scope:recipient
						}
					}
				}
				else_if = {
					limit = {
						scope:actor = {
							any_spouse = {
								OR = {
									is_close_family_of = scope:recipient
									is_nibling_of = scope:recipient
									is_uncle_or_aunt_of = scope:recipient
								}
							}
						}
					}
					scope:actor = {
						random_spouse = {
							limit = {
								OR = {
									is_close_family_of = scope:recipient
									is_nibling_of = scope:recipient
									is_uncle_or_aunt_of = scope:recipient
								}
							}
							save_scope_as = actor_spouse
						}
					}
					scope:actor = {
						create_alliance = {
							target = scope:recipient
							allied_through_owner = scope:actor
							allied_through_target = scope:actor_spouse
						}
					}
				}
				#...And finally marriage between close relatives and close relatives.
				else_if = {
					limit = {
						scope:actor = {
							any_close_family_member = {
								OR = {
									is_child_of = scope:actor
									is_parent_of = scope:actor
									is_sibling_of = scope:actor
								}
								any_spouse = {
									OR = {
										is_child_of = scope:recipient
										is_parent_of = scope:recipient
										is_sibling_of = scope:recipient
									}
								}
							}
						}
					}
					scope:actor = {
						random_close_family_member = {
							limit = {
								OR = {
									is_child_of = scope:actor
									is_parent_of = scope:actor
									is_sibling_of = scope:actor
								}
								any_spouse = {
									OR = {
										is_child_of = scope:recipient
										is_parent_of = scope:recipient
										is_sibling_of = scope:recipient
									}
								}
							}
							save_scope_as = actor_spouse
							random_spouse = {
								limit = {
									OR = {
										is_child_of = scope:recipient
										is_parent_of = scope:recipient
										is_sibling_of = scope:recipient
									}
								}
								save_scope_as = recipient_spouse
							}
						}
					}
					scope:actor = {
						create_alliance = {
							target = scope:recipient
							allied_through_owner = scope:actor_spouse
							allied_through_target = scope:recipient_spouse
						}
					}
				}
#				else_if = {
#					limit = {
#						scope:actor = { has_perk = defensive_negotiations_perk }
#					}
#					scope:actor = {
#						create_alliance = {
#							target = scope:recipient
#							allied_through_owner = scope:actor
#							allied_through_target = scope:recipient
#						}
#					}
#					scope:recipient = { # This opinion modifier controls the interaction, and is removed when breaking the alliance in any way (though on_actions)
#						add_opinion = {
#							modifier = perk_negotiated_alliance_opinion
#							target = scope:actor
#						}
#					}
#				}
				else_if = {
					limit = {
						scope:actor = {
							hostage_oath_of_friendship_trigger = yes
						}
					}
					scope:actor = {
						create_alliance = {
							target = scope:recipient
							allied_through_owner = scope:actor
							allied_through_target = scope:recipient
						}
					}
				}
			}
		}
	}
}
